package com.springboot.controller;

import com.springboot.dto.UploadOrderInfo;
import com.springboot.mapper.MessageInformMapper;
import com.springboot.model.*;
import com.springboot.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * 本控制台用于患者预约医生
 */
@Controller
public class PatientSelectOrderTimeController {
    @Autowired
    OrderDoctorService orderDoctorService;

    @Autowired
    UserLoginService userLoginService;

    @Autowired
    PatientOrderViewService patientOrderViewService;

    @Autowired
    UploadSicknessInfoService uploadSicknessInfoService;

    @Autowired
    FileUploadService fileUploadService;

    @Autowired
    PatientMyInformService patientMyInformService;

    /**
     * 当患者在预约点击上传时，更新预约表
     * (副本)
     * @param
     * @return
     */
    @CrossOrigin
    @RequestMapping(value = "/patientSelectOrderTime1")
    public @ResponseBody
    Map<String,String> patientSelectOrderTime1(@RequestParam("unitphone") String patientTel,
                                  @RequestParam("doctorId") Integer doctorId,
                                  @RequestParam("bookdate") Date bootDate,
                                  @RequestParam("bootTime") String bootTime) {
        System.out.println("患者上传预约详情");
        UserLoginForm userLoginForm = userLoginService.findUserByTel(patientTel);
        Integer patientId = userLoginForm.getUserId();
        OrderForm orderForm = new OrderForm(patientId, doctorId, bootDate);
        System.out.println(orderForm.getDoctorId()+" "+orderForm.getBootDate());
        System.out.println(orderForm.getPatientId()+" "+orderForm.getBootTime());
        Map<String,String> result = new HashMap<>();
        try {
            if(orderDoctorService.checkOrderForm(orderForm)==1)
            {
                result.put("messege","您已经预约过该天，请无重复预约");
                result.put("success","False");
            }
            else
            {
                orderForm.setBootTime(bootTime);
                orderDoctorService.insertOrderForm(orderForm);
                orderForm = orderDoctorService.findOrderForm(orderForm);
                DoctorAppointSetKey doctorAppointSetKey = new DoctorAppointSetKey(doctorId,bootDate);
                DoctorAppointSet doctorAppointSet = orderDoctorService.findDoctorAppointSet(doctorAppointSetKey);
                int i ;
                if(bootTime.equals("上午"))
                {
                    i=doctorAppointSet.getMorningNum()-1;
                    doctorAppointSet.setMorningNum(i);
                }
                else {
                    i=doctorAppointSet.getAfternoonNum()-1;
                    doctorAppointSet.setAfternoonNum(i);
                }
                orderDoctorService.updataDoctorAppointSet(doctorAppointSet);
                result.put("success","True");
                result.put("messege","恭喜您预约成功,您的预约编号是:");
                result.put("bookId",orderForm.getBootId()+"");
            }
        }
        catch (Exception e)
        {
            result.put("success","False");
            result.put("messege","出现错误，请联系管理员");
        }
        return result;
    }

    /**
     * 当用户进入上传病情描述页面时，返回医生姓名和可预约时间以及患者的信息
     */
    @CrossOrigin
    @RequestMapping(value = "/showOrderInfo")
    public @ResponseBody
    UploadOrderInfo showOrderInfo(@RequestParam("unitphone") String PatientTel,
                         @RequestParam("doctorId") Integer doctorId,
                         @RequestParam("bookdate") String bootDate1) throws ParseException {
        System.out.println("进入showOrderInfo");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date bootDate = null;
        try {
            System.out.println(bootDate1);
            java.util.Date bootDate2 = format.parse(bootDate1);
            bootDate = new java.sql.Date(bootDate2.getTime());
        }
        catch (Exception e)
        {
            System.out.println("字符串转java.sql.date错误");
        }
        UploadOrderInfo uploadOrderInfo = new UploadOrderInfo();
        try{
            UserLoginForm userLoginForm = userLoginService.findUserByTel(PatientTel);
            DoctorInfoForm doctorInfoForm = patientOrderViewService.findDoctorsById(doctorId);
            uploadOrderInfo.setDoctorName(doctorInfoForm.getDoctorName());
            uploadOrderInfo.setPatientName(userLoginForm.getUserName());
            uploadOrderInfo.setPatientSex(userLoginForm.getUserSex());
            DoctorAppointSetKey doctorAppointSetKey=new DoctorAppointSetKey(doctorId,bootDate);
            DoctorAppointSet doctorAppointSet = orderDoctorService.findDoctorAppointSet(doctorAppointSetKey);
            uploadOrderInfo.setMorningNum(doctorAppointSet.getMorningNum());
            uploadOrderInfo.setAfternoonNum(doctorAppointSet.getAfternoonNum());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        System.out.println(uploadOrderInfo.toTestString());
        return uploadOrderInfo;
    }

    /**
     * @ description:当患者在预约时点击上传按钮，更新预约表并发通知
     * @ param     :[patientTel, doctorId, bootDate, bootTime, sickSketch, file]
     * @ return    :java.lang.String
     * @ date       :2020/12/22

    */

    @CrossOrigin
    @RequestMapping(value = "/patientSelectOrderTime")
    public @ResponseBody
    String patientSelectOrderTime(@RequestParam("unitphone") String patientTel,
                                              @RequestParam("doctorId") Integer doctorId,
                                              @RequestParam("bookdate") Date bootDate,
                                              @RequestParam("bootTime") String bootTime,
                                              @RequestParam("sickSketch") String sickSketch,
                                              @RequestParam("file") MultipartFile file) {
        System.out.println("患者上传预约详情");
        System.out.println("病情描述："+sickSketch);
        UserLoginForm userLoginForm = userLoginService.findUserByTel(patientTel);
        Integer patientId = userLoginForm.getUserId();
        OrderForm orderForm = new OrderForm(patientId, doctorId, bootDate);
        Map<String,String> result = new HashMap<>();
        int flag=1;
        try
        {
            //已经预约过了
            if(orderDoctorService.checkOrderForm(orderForm)==1)
            {
                result.put("messege","您已经预约过该天，请无重复预约");
                result.put("success","False");
            }
            else
            {
                orderForm.setBootTime(bootTime);
                DoctorAppointSetKey doctorAppointSetKey = new DoctorAppointSetKey(doctorId,bootDate);
                DoctorAppointSet doctorAppointSet = orderDoctorService.findDoctorAppointSet(doctorAppointSetKey);
                System.out.println(doctorAppointSet.toTestString());
                int i ;
                if(bootTime.equals("上午")&&doctorAppointSet.getMorningNum()>0)
                {
                    i=doctorAppointSet.getMorningNum()-1;
                    doctorAppointSet.setMorningNum(i);
                }
                else if(bootTime.equals("下午")&&doctorAppointSet.getAfternoonNum()>0)
                {
                    System.out.println("下午");
                    i=doctorAppointSet.getAfternoonNum()-1;
                    doctorAppointSet.setAfternoonNum(i);
                }
                else {
                    System.out.println("该医生的时间段已经被预约完啦，请退出本页面再重新进入");
                    flag=0;
                    result.put("success","False");
                    result.put("messege","该医生的时间段已经被预约完啦，请退出本页面再重新进入");
                }

                if(flag==1)
                {
                    orderDoctorService.insertOrderForm(orderForm);
                    orderForm = orderDoctorService.findOrderForm(orderForm);
                    orderDoctorService.updataDoctorAppointSet(doctorAppointSet);
                    result.put("success","True");
                    result.put("messege","恭喜您预约成功,您的预约编号是:");
                    result.put("bookId",orderForm.getBootId()+"");
                    System.out.println("恭喜您预约成功,您的预约编号是:"+orderForm.getBootId());

                    PatientInfoForm patientInfoForm = new PatientInfoForm();
                    patientInfoForm.setBootId(orderForm.getBootId());
                    patientInfoForm.setSickSketch(sickSketch);
                    System.out.println("病情描述："+patientInfoForm.getSickSketch());
                    //图片上传，存在static下，相对路径为bookId=?/文件名，把图片路径存在数据库中
                    patientInfoForm.setSickInfo(fileUploadService.UploadPicture(file,"bookId="+orderForm.getBootId()));
                    uploadSicknessInfoService.insertPatientInfoForm(patientInfoForm);

                    //发通知
                    MessageInform messageInform = new MessageInform();
                    messageInform.setBookDate(bootDate);
                    messageInform.setBookTime(bootTime);
                    messageInform.setPatientId(patientId);
                    messageInform.setDoctorId(doctorId);
                    Integer messageMark = 1;
                    messageInform.setMessageMark(messageMark);
                    Integer messageId = patientMyInformService.insertMessage(messageInform);
                    patientMyInformService.insertAssistantMessageInformByDoctorId(doctorId,messageId);
                    return "True";
                }
            }
        }
        catch (Exception e)
        {
            result.put("success","False");
            result.put("messege","出现错误，请联系管理员");
        }
        return "False";
    }
}
